package com.test.service.impl;

import com.test.entity.Const;
import com.test.entity.dto.Account;
import com.test.entity.dto.storeImage;
import com.test.filter.FlowLimitFilter;
import com.test.mapper.AccountMapper;
import com.test.service.AccountService;
import com.test.service.ImageService;
import com.test.service.ImageStoreService;
import com.test.utils.FlowUtils;
import io.minio.*;
import io.minio.errors.*;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.logging.SimpleFormatter;

@Service
public class ImageServiceImpl implements ImageService {
    @Resource
    FlowUtils utils;
    @Resource
    MinioClient client;
    @Resource
    AccountMapper accountMapper;
    @Resource
    ImageStoreService imageStoreService;
    @Resource
    AccountService accountService;
    private final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    @Override
    public String uploadAvatar(MultipartFile file, int id) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        InputStream stream = file.getInputStream();
        String urlName = "avatar/"+ UUID.randomUUID();
        PutObjectArgs args = PutObjectArgs.builder()
                .bucket("study")
                .object(urlName)
                .stream(stream,file.getSize(),-1)
                .build();
        client.putObject(args);
        String avatar = accountMapper.selectById(id).getAvatar();
        if(avatar!=null)this.deleteImage(avatar);
        accountService.update().eq("id",id).set("avatar",urlName).update();
        return urlName;

    }
    public void fetchImage(String imageUrl, ServletOutputStream stream) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        GetObjectArgs args = GetObjectArgs.builder()
                .bucket("study")
                .object(imageUrl)
                .build();
        GetObjectResponse response = client.getObject(args);
        IOUtils.copy(response,stream);
        response.close();
    }

    @Override
    public String uploadImage(MultipartFile file, int id) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        if(!utils.limitPeriodCounterCheck(Const.FLOW_IMAGE_COUNT+id,100,3600))             return null;
        InputStream stream = file.getInputStream();
        Date date= new Date();
        String urlName = "image/"+formatter.format(date)+"/"+ UUID.randomUUID();
        PutObjectArgs args = PutObjectArgs.builder()
                .bucket("study")
                .object(urlName)
                .stream(stream,file.getSize(),-1)
                .build();
        client.putObject(args);
        imageStoreService.save(new storeImage(id,urlName,date));
        return urlName;
    }
    private void deleteImage(String urlName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        RemoveObjectArgs args = RemoveObjectArgs.builder()
                .bucket("study")
                .object(urlName)
                .build();
        client.removeObject(args);
    }

}
